#封装

#封装的第一个阶段
class User:
    def __init__(self,name,age,address,course,grade=None):
        self.name=name
        self.age=age
        self.address=address
        self.course=course
        self.grade=grade

u=User("1",2,3,4,5)

def work(name,age,address,course,grade):
    pass

def work1(user):
    print(user.name)
    print(user.age)
    print(user.grade)
work1(u)

#第二个阶段，把参数隐藏起来
class User1:
    def __init__(self,name,age,address,course,grade=None):
        self.__name=name
        self.__age=age
        self.__address=address
        self.__course=course
        self.__grade=grade

u1=User1("1",2,3,4)
print("u1.name",u1._User1__name)

#第三个阶段，定义get和set方法
class User2:
    def __init__(self,name,age,address,course,grade=None):
        self.__name=name
        self.__age=age
        self.__address=address
        self.__course=course
        self.__grade=grade

    def get_name(self):
        return self.__name

    def set_name(self,name):
        if isinstance(name,str):
            self.__name=name
        else:
            self.__name=str(name)

    def get_age(self):
        return self.__age

    def set_age(self,age):
        if isinstance(age,int):
            self.__age=age
        else:
            self.__age=int(age)

    def get_address(self):
        return self.__address

    def set_address(self,address):
        if isinstance(address,str):
            self.__name=address
        else:
            self.__name=str(address)




print("===========")


u2 = User2("xiaowang", 2, 3, 4)
# print(u2.get_name())
#
# u2.set_name("xiaozhang")
# print(u2.get_name())
#
# u2.set_name(123)
# print(u2.get_name())
# print(type(u2.get_name()))


u2.set_age("16")
print(u2.get_age())
print(type(u2.get_age()))


#魔法方法
print(u)
#把对象转为json
# print(u.__dict__)
# print(type(u.__dict__))
#
# print(u.__getattribute__("name"))
# print(u.__delattr__("name"))
# print(u.__str__())


